import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;


public class MyObjectPool {
	/**
	 * objects - array of objects
	 * ind[i] - indicates employment of objects[i] : if 0 - free, else 1 
	 */
	private BlockingQueue<Object> objects;
	private int size;
	public MyObjectPool(int startSize) {
		this.size = 0;
		objects = new LinkedBlockingQueue<Object>();
	}
	public synchronized Object getObject() {
		try {
			while (true) {
				if (!objects.isEmpty()) {
					return objects.take();
				}
				wait();
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return null;
	}
	public synchronized void setObject(Object object) {
		if (object == null) 
			return;
		objects.add(object);
		notifyAll();
	}
	public synchronized void clearPool() {
		for (int i = 0; i < size; i++) {
			try {
				objects.take();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}
